﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using CodeGenerator.Implementations;
using CodeGenerator.interfaces;

namespace CodeGenerator
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //StartGeneration();
            //Process.GetCurrentProcess().Kill();
        }

        public enum ProviderType
        {
            SqlLite,
            SQLServer
        }
        private void button2_Click(object sender, EventArgs e)
        {
            
            switch (cmb_DbType.Text)
            {
                case "SQLite Database File":
                {
                        //try
                        //{
                            //txt_Database.Text), txt_OutputFolder.Text
                            if (!File.Exists(txt_SqlIteDatabase.Text))
                            {
                                MessageBox.Show(
                                    "The file \r\n\r\n" + txt_SqlIteDatabase.Text + "\r\n\r\ndoes not exists!", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                return;
                            }

                            if (!Directory.Exists(txt_OutputFolder.Text))
                            {
                                MessageBox.Show(
                                    "The folder \r\n\r\n" + txt_OutputFolder.Text + "\r\n\r\ndoes not exists!", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                return;
                            }

                            string mainProjectPath = txt_OutputFolder.Text;
                            if (!mainProjectPath.EndsWith(@"\"))
                                mainProjectPath += @"\";

                            mainProjectPath = mainProjectPath + txt_Namespace.Text;

                            string testProjectPath = mainProjectPath + ".SampleProject";

                            if (!Directory.Exists(mainProjectPath))
                                Directory.CreateDirectory(mainProjectPath);

                            if (!Directory.Exists(testProjectPath))
                                Directory.CreateDirectory(testProjectPath);

                            StartGeneration(DatabaseEngines.SqlLite);
                            MessageBox.Show("Generation process completed successfully", "Generation",
                                            MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                                            MessageBoxDefaultButton.Button1);
                        //}
                        //catch (Exception ex)
                        //{
                        //    MessageBox.Show(ex.Message + "(" + ex.StackTrace + ")");
                        //}
                        break;

                    }

                case "SQL Server 2005":
                    {
                        
                        //try
                        //{
                            string mainProjectPath = txt_OutputFolder.Text;
                            if (!mainProjectPath.EndsWith(@"\"))
                                mainProjectPath += @"\";

                            mainProjectPath = mainProjectPath + txt_Namespace.Text;

                            string testProjectPath = mainProjectPath + ".SampleProject";

                            if (!Directory.Exists(mainProjectPath))
                                Directory.CreateDirectory(mainProjectPath);

                            if (!Directory.Exists(testProjectPath))
                                Directory.CreateDirectory(testProjectPath);

                            StartGeneration(DatabaseEngines.SqlServer);
                            MessageBox.Show("Generation process completed successfully", "Generation",
                                            MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                                            MessageBoxDefaultButton.Button1);
                        //}
                        //catch (Exception ex)
                        //{
                        //    MessageBox.Show(ex.Message + "(" + ex.StackTrace + ")");
                        //}
                        break;
                    }
                    default:
                    {
                        MessageBox.Show("No Database type selected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
            }
        }

        private void StartGeneration(DatabaseEngines DbEngine)
        {
            GeneratorModel gm = new GeneratorModel(txt_Namespace.Text, DbEngine);
            switch (DbEngine)
            {
                case DatabaseEngines.SqlLite:
                    gm.LoadModel(new SQLiteDatabaseProvider(gm, txt_SqlIteDatabase.Text), txt_OutputFolder.Text);
                    break;
                case DatabaseEngines.SqlServer:
                    gm.LoadModel(new SQLServerDatabaseProvider(gm, txt_SqlServer_server.Text, txt_SqlServer_Database.Text, txt_SqlServer_Username.Text, txt_SqlServer_Password.Text, chk_SqlServer_TrustedConnection.Checked), txt_OutputFolder.Text);
                    break;
            }
            gm.SaveSourceCode(chk_GenerateProjectFile.Checked, false); // chk_GenerateProjectSample.Checked); 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txt_OutputFolder.Text = folderBrowserDialog1.SelectedPath;
        }

        private void txt_OutputFolder_TextChanged(object sender, EventArgs e)
        {
      
        }

        private void txt_OutputFolder_DoubleClick(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = txt_OutputFolder.Text;
            p.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(File.Exists(txt_SqlIteDatabase.Text))
                openFileDialog1.InitialDirectory = (new FileInfo(txt_SqlIteDatabase.Text)).DirectoryName ;

            openFileDialog1.ShowDialog();
            txt_SqlIteDatabase.Text = openFileDialog1.FileName;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
                txt_SqlServer_Username.Enabled = !chk_SqlServer_TrustedConnection.Checked;
                txt_SqlServer_Password.Enabled = !chk_SqlServer_TrustedConnection.Checked;
        }

        private void cmb_DbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmb_DbType.SelectedItem.ToString())
            {
                case "SQLite Database File":
                    {
                        txt_SqlServer_Username.Enabled = false;
                        txt_SqlServer_server.Enabled = false;
                        txt_SqlServer_Password.Enabled = false;
                        txt_SqlServer_Database.Enabled = false;
                        chk_SqlServer_TrustedConnection.Enabled = false;

                        txt_SqlIteDatabase.Enabled = true;
                        btn_Sqlite_SearchDB.Enabled = true;
                        break;
                    }
                case "SQL Server 2005":
                    {
                        txt_SqlServer_Username.Enabled = true;
                        txt_SqlServer_server.Enabled = true;
                        txt_SqlServer_Password.Enabled = true;
                        txt_SqlServer_Database.Enabled = true;
                        chk_SqlServer_TrustedConnection.Enabled = true;

                        txt_SqlIteDatabase.Enabled = false;
                        btn_Sqlite_SearchDB.Enabled = false;
                        break;
                    }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            txt_SqlServer_server.Text = @"BWS13177\SQLEXPRESS";
            chk_SqlServer_TrustedConnection.Checked = true;
            txt_OutputFolder.Text =@"p:\Seba\work\MusicTransporter\EasyChords.de.DataAccess";

        }

        


    }
}
